﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace GUI
{
    public partial class Form3 : Form
    {
        double totalPrice;
        DateTime travelDate;
        List<RouteServiceRef.Flightroute> list;
        bool loggedIn = false;

        RouteServiceRef.TicketReservation ticket;

        public Form3()
        {
            InitializeComponent();
            this.Text = "RouteApp";
            SetDropdownValues();
        }

        void SetDropdownValues()
        {
            try
            {
                using (RouteServiceRef.RouteCtrClient rcc = new RouteServiceRef.RouteCtrClient())
                {
                    foreach (var item in rcc.GetAllAirports())
                    {
                        comboBoxFrom.Items.Add(item.name);
                        comboBoxTo.Items.Add(item.name);
                    }


                }
                comboBoxFrom.Text = comboBoxFrom.Items[0].ToString();
                comboBoxTo.Text = comboBoxTo.Items[0].ToString();
            }
            catch (Exception e)
            {
                MessageBox.Show("Something went wrong in trying to connect to the service. Exception thrown: " + e.Message);
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            panelRoute.Visible = true;
            panelSearch.Visible = false;


            try
            {
                using (RouteServiceRef.RouteCtrClient reserve_buyClient = new RouteServiceRef.RouteCtrClient())
                {
                    StringBuilder sb = new StringBuilder();
                    double total = 0;

                    list = new List<RouteServiceRef.Flightroute>();
                    list.AddRange(reserve_buyClient.GetCheapestRoute(comboBoxFrom.Text, comboBoxTo.Text, (int)numericUpDown1.Value, dateTimePicker1.Value));


                    List<int> tempList = new List<int>();
                    foreach (var item in list)
                    {
                        tempList.Add(item.routeId);
                    }

                    int[] arraylist = tempList.ToArray();

                    ticket = reserve_buyClient.ReserveTickets(arraylist, (int)numericUpDown1.Value);

                    foreach (var item in list)
                    {
                        sb.AppendLine("Port of departure: " + item.departure);
                        sb.AppendLine("Port of destination: " + item.destination);
                        sb.AppendLine("Time of departure: " + item.timeOfDep.Value.ToShortDateString());
                        sb.AppendLine("Price: " + item.price);
                        sb.AppendLine();
                        sb.AppendLine();
                        total += (double)item.price;
                    }
                    totalPrice = total;

                    sb.AppendLine("Total: " + total);

                    textBox1.Text = sb.ToString();
                }
            }
            catch (Exception e2)
            {
                textBox1.Text = "Der opstod en fejl. " + e2.Message;
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            panelSearch.Visible = true;
            panelRoute.Visible = false;
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            CustomerForm d = new CustomerForm();
            d.Show();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBoxUsername.Text) || !string.IsNullOrWhiteSpace(textBoxPass.Text))
            {
                try
                {
                    using (CustomerServiceRef.CustomerCtrClient csf = new CustomerServiceRef.CustomerCtrClient())
                    {
                        if (csf.GetCustomer(textBoxUsername.Text).pass == textBoxPass.Text)
                        {
                            //MessageBox.Show("You are logged in as " +textBoxUsername.Text, "LogIn information");

                            labelUser.Text = textBoxUsername.Text;
                            loggedIn = true;
                            panelUser.Visible = true;
                            panelLogin.Visible = false;
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("The username or password was incorrect!", "LogIn information");
                }
            }
        }

        private void buttonBuy_Click(object sender, EventArgs e)
        {
            if (loggedIn == true)
            {
                try
                {
                    //using (OrderServiceRef.OrderCtrClient osf = new OrderServiceRef.OrderCtrClient())
                    //{
                    //    List<int> tempList = new List<int>();
                    //    foreach (var item in list)
                    //    {
                    //        tempList.Add(item.routeId);
                    //    }

                    //    osf.CreateOrder(textBoxUsername.Text, totalPrice, DateTime.Now, (DateTime)list[0].timeOfDep, tempList.ToArray());
                    //}

                    using (RouteServiceRef.RouteCtrClient reserve_buyClient = new RouteServiceRef.RouteCtrClient())
                    {
                        reserve_buyClient.BuyTicket(ticket);
                    }
                    
                    MessageBox.Show("Tickets successfully purchased", "Purchase Confirmation");
                    panelSearch.Visible = true;
                    panelRoute.Visible = false;
                }
                catch (Exception e2)
                {
                    MessageBox.Show("Error-message: " + e2.Message, "ERROR!");
                }
            }
            else
            {
                MessageBox.Show("The username or password was incorrect!", "LogIn information");
            }
        }

        private void buttonLogout_Click(object sender, EventArgs e)
        {
            loggedIn = false;

            textBoxUsername.Text = "SSN";
            textBoxPass.Text = "Password";

            panelUser.Visible = false;
            panelLogin.Visible = true;
        }
    }
}
